<?php

namespace app\stores\helper\payment;


use app\beautiful\model\LevelModel;

use app\common\helper\payment\PaymentPlatformBaseHelper;

use app\stores\model\order\LevelOrderModel;

//每个子类只需要专注于自己的 respond 逻辑

class PaymentLevelOrderHelper extends PaymentPlatformBaseHelper {

    protected  $moudel = 'stores';

    protected  $orderKey = 'mo'; //给一个KEY增大吞吐量

    protected  $respondAction = 'levelRespond';

    

    public function respondCallback($orderId,$payInfo,$fail){

        $order = LevelOrderModel::find($orderId);

        if(null === $order) return;      

        if($order->is_paid == 0){

            $order->is_paid  = 1;

            $order->pay_info = $payInfo;

            $order->pay_type = 'weixin';

            $order->status = 1;

            $order->pay_time = date('Y-m-d H:i:s');

            $order->edit_time = date('Y-m-d H:i:s');

            if(empty($order->mix_pay_type)) $order->weixin = $order->pay_money;

            $order->save();

            //混合支付扣除其他支付方式的余额
            $return = mix_pay_money($order,$payInfo,3);
            //不用退款则按正常支付成功流程走
            if($return['is_refund']<1){
                //修改会员有效时间
                $return = memberVipTime($order);
                if(!$return['code']){
                    return  $this->error($return['error']);
                }
                $level_model = new LevelModel();

                $level = $level_model->getById(['level_id'=>$order->level_id,'is_delete'=>0]);
                //领券
                memberVipCoupon($order,$level);

            }else{
                //自动退款后通知用户

            }


        }

    }

}